package com.example.day16_mp;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.text.SimpleDateFormat;
import java.util.List;

public class MusicAdapter extends BaseAdapter {
    List<Music.DataBean> list;
    Context context;

    public MusicAdapter(List<Music.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder = new MyViewHolder();
        if(convertView==null){
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_music,null);
            myViewHolder.itemIv = convertView.findViewById(R.id.item_iv);
            myViewHolder.itemLove = convertView.findViewById(R.id.item_love);
            myViewHolder.itemName = convertView.findViewById(R.id.item_name);
            myViewHolder.itemTime = convertView.findViewById(R.id.item_time);
            convertView.setTag(myViewHolder);
        }
        myViewHolder = (MyViewHolder) convertView.getTag();
        myViewHolder.itemName.setText(list.get(position).getName());
        SimpleDateFormat format = new SimpleDateFormat("mm:ss");
        myViewHolder.itemTime.setText(format.format(list.get(position).getDuration()));
        Glide.with(context).load(list.get(position).getPicUrl()).into(myViewHolder.itemIv);
        //点击收藏
        myViewHolder.itemLove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("sss", "onClick: 收藏这个音乐");
                SqlUtils.getInstance(parent.getContext()).insert(list.get(position));
                ((ImageView)v).setImageResource(R.drawable.baseline_play_circle_outline_24);
            }
        });

        return convertView;
    }

    class MyViewHolder{
        ImageView itemIv,itemLove;//封面图片   收藏图标
        TextView itemName,itemTime;//歌曲名称  歌曲时长
    }
}
